
'use strict';

const express=require('express');
const path=require('path');
const xtpl=require('xtpl');
const moment=require('moment');

const db=require(path.join(__dirname,"../configs/database.js"));
const base=require(path.join(__dirname,"./baseController.js"));

module.exports.teaMessList=(req,res)=>{
  
const page=parseInt(req.query.page||'1');
const limit=parseInt(req.query.limit||'12');

const start=(page-1)*limit;

db.table('message').count("*").then(function(count){
  const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
  const totalPageArray=[];
  for(var i=1;i<=totalPage;i++){
    totalPageArray.push(i);
  };
  db.table('message').join("pre_student on pre_message.stu_id=pre_student.stu_id").join("pre_teach on pre_message.teach_id=pre_teach.teach_id").join("pre_class on pre_message.class_id=pre_class.class_id").order("message_id DESC").limit(start,limit).select().then(function(data){
    data.message_time=moment(data.message_time*1000).format("YYYY-MM-DD");
   xtpl.renderFile(
   path.join(__dirname,"../views/admin/teaMess_list.html"),
   {
     mes:data,
     totalPage:totalPage,
     totalPageArray:totalPageArray,
     page:page
   },
   function(err,content){
       res.setHeader("Content-Type","text/html;charset=utf-8");
       res.end(content);
   }
 	);
  });
});

};

module.exports.teaMessDelete=(req,res)=>{

   let message_id=req.params.message_id;
   let where=`message_id=${message_id}`;
   
   db.table('message').where(where).delete().then(
     
      (messageRows)=>{

      	 base.showMsg(res,'删除成功','/teaMess/teaMess_list');
      	 return false;
      }

   	).catch(

       (err)=>{

           if(err){

           	base.showMsg(res,'删除失败','/teaMess/teaMess_list');
           	return false;
           }

       }
   	);

  






};